var $http = axios.create({
    baseURL: 'http://localhost:3000'
})


var tuijianObj = {
    getList() {
        var data = {
            params: {
                _sort: "cTime",
                _order: "desc",
            }
        }
        $http.get('/recommendList', data).then(res => {
            console.log(res);
            tuijianObj.render(res.data);
            tuijianObj.list = res.data;
        })
    },

    bindEvents() {

        $('#list').on('click', '.del', function () {
            var index = $(this).closest('tr').index();
            var id = tuijianObj.list[index - 1].id;
            $http.delete("/recommendList/" + id).then(res => { 
                // 根据.del元素找到父元素li并删除
                $(this).closest('tr').remove();
            }).catch(err => {
                console.log(err);
            })
        })
    },


    render(list) {
        var htmlStr = '';
        list.forEach(function (item, index) {
            htmlStr += `
            <tr class=""> 
                <td class="">${item.id}</td>
                <td class="">${common.formateDate(item.cTime)}</td>
                <td class="">${common.formateDate(item.uTime||item.cTime)}</td>
                <td class="">${item.name}</td>
                <td class=""><img class="img" src="${item.icon}"/> </td>
                <td class="">
                    <button class="del">删除</button>
                    <button class="submit ml-5"><a class="fff" href="./edit.html?id=${item.id}">编辑</a></button>
                </td>
            </tr>`;
        })
        $('tbody').html(htmlStr);
    }
}

tuijianObj.getList();
tuijianObj.bindEvents();